﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PKSpeed
{
    /// <summary>
    /// 用户信息
    /// </summary>
    public class AliasCommand
    {
        public String UserName { get; set; }
        public String AliasCommandName { get; set; }
        public String OldCommandString { get; set; }
    }
    /// <summary>
    /// Alias配置文件类
    /// </summary>
    public class AliasConfiguration
    {
        /// <summary>
        /// 添加Alias等效命令
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="aliasCommandName">等效命令</param>
        /// <param name="oldCommandString">原命令</param>
        /// <returns></returns>
        public static bool AddAliasCommand(String userName, String aliasCommandName, String oldCommandString)
        {
            String cmd = GetOldCommand(userName, aliasCommandName);
            if (cmd != null)//如果该等效命令已经存在
            {
                return false;
            }
            String path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//获取app.config路径
            XmlDocument xml = new XmlDocument();
            xml.Load(path);
            XmlNodeList aliasCmdList = xml.GetElementsByTagName(userName);
            if (aliasCmdList.Count == 0)//如果用户没有创建过Alias命令
            {
                XmlNode xmlNode = xml.GetElementsByTagName("configuration")[0];
                xmlNode.AppendChild(xml.CreateNode(XmlNodeType.Element, userName, ""));
                aliasCmdList = xml.GetElementsByTagName(userName);
            }
            XmlNode aliasCmds = aliasCmdList[0]; //获取userName节点下内容
            XmlElement aliasCmdElement = xml.CreateElement("command");//创建命令元素
            //设置元素属性
            aliasCmdElement.SetAttribute("AliasCommandName", aliasCommandName);
            aliasCmdElement.SetAttribute("OldCommandString", oldCommandString);
            //添加元素
            aliasCmds.AppendChild(aliasCmdElement);
            //保存文件
            xml.Save(path);

            return true;
        }

        /// <summary>
        /// 根据用户名和等效命令获取原命令
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="aliasCmd">等效命令</param>
        /// <returns>原命令</returns>
        public static String GetOldCommand(String userName, String aliasCmd)
        {
            String path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//获取app.config路径
            List<AliasCommand> userlist = new List<AliasCommand>(); //将等效命令信息用AliasCommand类保存，返回使用
            XmlDocument xml = new XmlDocument();
            xml.Load(path); //读取config文件
            XmlNodeList aliasCmdList = xml.GetElementsByTagName(userName);
            if (aliasCmdList.Count == 0)
            {
                return null;
            }
            XmlNode aliasCmds = aliasCmdList[0]; //获取userName节点下内容
            XmlNodeList allCmds = aliasCmds.SelectNodes("command"); //获取config所有command节点
            if (allCmds == null || allCmds.Count == 0)
            {
                return null;
            }
            //返回符合条件的属性值，如找不到则返回null
            return (allCmds.Cast<XmlNode>().Where(
                c => c.Attributes != null && c.Attributes["AliasCommandName"].Value == aliasCmd).Select(
                    c => c.Attributes != null ? c.Attributes["OldCommandString"].Value : null)).FirstOrDefault();
        }

        /// <summary>
        /// 得到用户所有的等效命令
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>等效命令列表</returns>
        public static List<String> GetAllCommand(String userName)
        {
            String path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; //获取app.config路径
            List<AliasCommand> userlist = new List<AliasCommand>(); //将等效命令信息用AliasCommand类保存，返回使用
            XmlDocument xml = new XmlDocument();
            xml.Load(path); //读取config文件
            XmlNodeList aliasCmdList = xml.GetElementsByTagName(userName);
            if (aliasCmdList.Count == 0)
            {
                return null;
            }
            XmlNode aliasCmds = aliasCmdList[0]; //获取userName节点下内容
            XmlNodeList allCmds = aliasCmds.SelectNodes("command"); //获取config所有command节点
            if (allCmds == null || allCmds.Count == 0)
            {
                return null;
            }
            //返回符合条件的属性值，如找不到则返回null
            return (allCmds.Cast<XmlNode>().Where(a => a.Attributes != null).Select(
                a => a.Attributes != null ? a.Attributes["AliasCommandName"].Value : null)).
                    ToList();
        }

        /// <summary>
        /// 覆盖Alias等效的命令
        /// </summary>
        /// <param name="userName">用户</param>
        /// <param name="aliasCmd">等效命令</param>
        /// <param name="newOldCmd">新的原命令</param>
        /// <returns>是否成功</returns>
        public static bool ModifyAliasCommand(String userName, String aliasCmd, String newOldCmd)
        {
            String path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//获取app.config路径
            List<AliasCommand> userlist = new List<AliasCommand>(); //将等效命令信息用AliasCommand类保存，返回使用
            XmlDocument xml = new XmlDocument();
            xml.Load(path); //读取config文件
            XmlNodeList aliasCmdList = xml.GetElementsByTagName(userName);
            if (aliasCmdList.Count == 0)
            {
                return false;
            }
            XmlNode aliasCmds = aliasCmdList[0]; //获取userName节点下内容
            XmlNodeList allCmds = aliasCmds.SelectNodes("command"); //获取config所有command节点
            if (allCmds == null || allCmds.Count == 0)
            {
                return false;
            }
            //返回符合条件的属性值，如找不到则返回null
            XmlNode theNode = (allCmds.Cast<XmlNode>().Where(
                c => c.Attributes != null && c.Attributes["AliasCommandName"].Value == aliasCmd).Select(
                    c => c)).FirstOrDefault();
            if (theNode.Attributes != null)
            {
                theNode.Attributes["OldCommandString"].Value = newOldCmd;
            }
            //保存文件
            xml.Save(path);
            return true;
        }

        /// <summary>
        /// 删除用户时调用此方法删除该用户所有的等效命令设置
        /// </summary>
        /// <param name="userName">用户名</param>
        public static void DeleteUserWithAliasCommand(String userName)
        {
            String path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//获取app.config路径
            List<AliasCommand> userlist = new List<AliasCommand>(); //将等效命令信息用AliasCommand类保存，返回使用
            XmlDocument xml = new XmlDocument();
            xml.Load(path); //读取config文件
            XmlNodeList aliasCmdList = xml.GetElementsByTagName(userName);
            if (aliasCmdList.Count == 0)//该用户未设置等效命令
            {
                return;
            }
            xml.RemoveChild(aliasCmdList[0]);
        }
    }
}
